#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
bool ok;
int tot;
int a[7];

void search(int n)
{
    if(n!=8)
    {
        for(int i=1;i<=8;i++)
        {
          a[n]=i;
          int ok =1;
          for (int j=0;j<n;j++)
          {
              if(a[j]==i||a[j]==i-n+j||a[j]==i+n-j) ok=0;
          }
          if (ok) search(n+1);
        }
    }
    else
    {
        for(int i=0;i<8;i++)
            cout<<a[i]<<" ";
        cout<<"\n";
        ++tot;
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    search(0);
    cout<<tot;
    return a.exec();
}
